package com.geovis.emergency.spd.controller.pc;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.constant.SwaggerConstants;
import com.geovis.common.core.controller.BaseController;
import com.geovis.emergency.spd.biz.basic.service.IBasicKpiService;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillKpiService;
import com.geovis.emergency.spd.entity.basic.entity.BasicKpi;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillKpi;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcDrillKpiVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 基础模块-演练考核指标 前端控制器
 * </p>
*
* @author Lonsho
* @since 2022-07-26
*/
@RestController
@RequestMapping("/pc/pcDrillKpi")
@RequiredArgsConstructor
@Api(value = "基础模块-演练考核指标接口", tags = "基础模块-演练考核指标接口")
@Slf4j
@Validated
public class PcDrillKpiController extends BaseController<IPcDrillKpiService> {
    private final IBasicKpiService basicKpiService;
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = SwaggerConstants.LIST_VALUE, notes = SwaggerConstants.LIST_NOTES)
    @PostMapping({"/list/{drillId}"})
    public Result<List<PcDrillKpiVO>> list(@PathVariable("drillId") String drillId){
        List<PcDrillKpi> list = baseService.list(
                Wrappers.lambdaQuery(PcDrillKpi.class)
                        .eq(PcDrillKpi::getPcDrillId, drillId)
                        .orderByAsc(PcDrillKpi::getIdx)
        );
        if (ObjectUtil.isNotEmpty(list)){
            return Result.ok(list.stream().map(kpi -> BeanUtil.toBean(kpi, PcDrillKpiVO.class)).collect(Collectors.toList()));
        }else {
            List<BasicKpi> baseList = basicKpiService.list(
                    Wrappers.lambdaQuery(BasicKpi.class)
                            .orderByAsc(BasicKpi::getIdx)
            );
           return Result.ok(baseList.stream().map(basicKpi -> {
                PcDrillKpiVO pcDrillKpi = BeanUtil.toBean(basicKpi, PcDrillKpiVO.class);
                pcDrillKpi.setId(null);
                pcDrillKpi.setPcDrillId(drillId);
                pcDrillKpi.setKpiId(basicKpi.getId());
                return pcDrillKpi;
            }).collect(Collectors.toList())
           );
        }
    }
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = SwaggerConstants.SAVE_VALUE, notes = SwaggerConstants.SAVE_NOTES)
    @PostMapping({"/submit"})
    public Result submit(@RequestBody List<PcDrillKpiVO> drillKpiVOList){
        baseService.saveOrUpdateBatch(drillKpiVOList);
        return Result.ok();
    }
}
